import { _decorator, Collider, Component, ITriggerEvent, Node, sp } from 'cc';
import { Contant } from '../framework/Contant';
import { gameManger } from '../framework/gameManger';
const { ccclass, property } = _decorator;

@ccclass('BulletProp')
export class BulletProp extends Component {

    private _gameManager: gameManger = null;
    private _propSpeed = 0.3;
    private _propXSpeed = 0.3;

    update(deltaTime: number) {
        let pos = this.node.position
        if(pos.x >= 13){
            this._propXSpeed = this._propSpeed
        }else if(pos.x <= -19){
            this._propXSpeed = -this._propSpeed
        }
        this.node.setPosition(pos.x + this._propXSpeed, pos.y, pos.z - this._propSpeed);
        pos = this.node.position;
        if(pos.z > 50){
            console.log("被销毁了",pos)
            this.node.destroy()
        }
    }

    protected onEnable(): void {
        const collir = this.getComponent(Collider)
        collir.on("onTriggerEnter", this._onTriggerEnter, this)
    }

    protected onDisable(): void {
        const collir = this.getComponent(Collider)
        collir.on("onTriggerEnter", this._onTriggerEnter, this)
    }

    public show(gameManger : gameManger, speed:number) {
        this._gameManager = gameManger
        this._propSpeed = speed
    }

    _onTriggerEnter(event: ITriggerEvent){
        console.log("活动奖励")
        let bulletName = event.selfCollider.node.name
        if(bulletName === 'bulletH'){
            this._gameManager.changeBulletType(Contant.BulletPropType.BULLET_H)
        }else if (bulletName === 'bulletM'){
            this._gameManager.changeBulletType(Contant.BulletPropType.BULLET_M)
        }else {
            this._gameManager.changeBulletType(Contant.BulletPropType.BULLET_S)
        }
        this.node.destroy()
    }
}


